﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.SessionState;
using com.nm.library.Excels;
using com.nm.Helper;

namespace com.nm.library.HttpHelper
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseHttpHandler: IHttpHandler,IReadOnlySessionState
    {
        /// <summary>
        /// 指定过来的http请求类型  主要指定action方法名称的接收方式 get 或者 post
        /// </summary>
        protected NameValueCollection _httpReuqest = HttpContext.Current.Request.Form;

        /// <summary>
        /// 指定返回头
        /// </summary>
        protected string _contentType = "text/plain";

        /// <summary>
        /// 指定接收action方法的参数名称
        /// </summary>
        protected string _actionName = "action";

        //获取当前的http context
        protected HttpContext Context => HttpContext.Current;

        /// <summary>
        /// 
        /// </summary>
        public  IEnumerable<ExcelImport> AllImports { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = this._contentType;
            try
            {
                //动态调用方法 当然  你还可以在这里加上是否为同域名请求的判断
                this.DynamicMethod();
            }
            catch (Exception amEx)
            {
                throw amEx;
            }
        }

        #region 动态调用方法
        /// <summary>
        /// 动态调用方法
        /// </summary>
        private void DynamicMethod()
        {
            //根据指定的请求类型获取方法名
            var action = this._httpReuqest[this._actionName];
            if (!string.IsNullOrEmpty(action))
            {
                //获取方法的实例  非静态 需要Public访问权限 忽略大小写
                var methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (methodInfo != null)
                {
                    //调用方法
                    methodInfo.Invoke(this, null);
                }
                else
                {
                    throw new ApplicationException($"没有找到方法{action}");
                }
            }
            else
            {
                throw new ArgumentNullException($"没有找到调用方法参数或者方法名为空");
            }
        }
        #endregion

        /// <summary>
        /// 打印json
        /// </summary>
        /// <param name="state"></param>
        /// <param name="msg"></param>
        protected void output(object r)
        {
            var response = this.Context.Response;

            SetHeader(response);
            response.AddHeader("Content-Type", "application/json");
            response.Write(r.ToJson());
        }

        internal static void SetHeader(HttpResponse Response)
        {
            Response.ClearHeaders();
            Response.AddHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,Access-Control-Request-Method,X_FILENAME");
            Response.AddHeader("Access-Control-Allow-Methods", "GET,PUT,POST,HEAD,DELETE,OPTIONS");
            Response.AddHeader("Access-Control-Max-Age", "360000");
            Response.AddHeader("Access-Control-Allow-Credentials", "true");
            //Response.AddHeader("Access-Control-Allow-Origin", "*");
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsReusable => false;
    }
}
